<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Service\InfoService;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class InfoController extends CommonController
{ 
    #[Inject]
    private InfoService $service;

    #[GetMapping('listInfos')]
    public function listInfos()
    {
        $data = $this->valid([
            'id' => 'string|regex:/^\d+(,\d+)*$/',
            'group' => 'integer|required',
            'typeId' => 'integer',
            'spotId' => 'integer',
            'status' => 'integer',
            'sort' => 'integer',
            'page' => 'integer|min:1',
            'pageSize' => 'integer:min:1|max:100',
        ], []);
        return $this->success($this->service->list($data));
    }

    #[PostMapping('addInfo')]
    public function addInfo()
    {
        $data = $this->valid([
            'title' => 'required|string',
            'intro' => 'string',
            'coverId' => 'string',
            'group' => 'integer',
            'typeId' => 'integer',
            'spotId' => 'integer',
            'status' => 'integer',
            'sort' => 'integer',
            'extension' => 'array'
        ], []);
        return empty($res = $this->service->create($data)) ? $this->error() : $this->success($res);
    }

    #[GetMapping('deleteInfo')]
    public function deleteInfo()
    {
        $data = $this->valid(['id' => 'required|regex:/^\d+(,\d+)*$/'], []);
        $ids = explode(',', $data['id']);
        return $this->success($this->service->delete($ids));
    }

    #[PostMapping('updateInfo')]
    public function updateAttraction()
    {
        $data = $this->valid([
            'id' => 'required|integer',
            'title' => 'required',
            'intro' => 'string',
            'coverId' => 'string',
            'group' => 'integer',
            'typeId' => 'integer',
            'spotId' => 'integer',
            'status' => 'integer',
            'sort' => 'integer',
            'extension' => 'array'
        ], []);
        return $this->success($this->service->update(intval($data['id']), $data));
    }

    #[GetMapping('detailInfo')]
    public function detailInfo()
    {
        $data = $this->valid(['id' => 'required|integer'], []);
        return $this->success($this->service->get(intval($data['id'])));
    }

    #[GetMapping('listValidInfos')]
    public function listValidInfos()
    {
        return $this->success($this->service->listValidInfos(
            $this->valid([
                'isValid' => 'required|integer|in:1,2',
                'page' => 'integer|min:1',
                'pageSize' => 'integer:min:1|max:100',
            ], []))
        );
    }
}